<!doctype html>
<!--
Tests that screen taps when using Cardboard are translated into WebXR input, and
that Daydream controller input is registered when using Daydream View.
-->
<html>
  <head>
    <link rel="stylesheet" type="text/css" href="../resources/webxr_e2e.css">
  </head>
  <body>
    <canvas id="webgl-canvas"></canvas>
    <script src="../../../../../../third_party/blink/web_tests/resources/testharness.js"></script>
    <script src="../resources/webxr_e2e.js"></script>
    <script src="../resources/webxr_boilerplate.js"></script>
    <script>
      setup({single_test: true});

      var selectStartCount = 0;
      var selectEndCount = 0;
      var selectCount = 0;
      var iterations;
      var currentIteration = 0;
      var finishAfterEachInput = true;
      var expectedInputSourceIndex = 0;

      function getExpectedInputSource(ev) {
        return ev.frame.session.inputSources[expectedInputSourceIndex];
      }

      function onSelectStart(ev) {
        assert_equals(ev.inputSource, getExpectedInputSource(ev));
        // selectstart should always be fired first, so check that.
        assert_true(selectStartCount == selectEndCount,
                    'selectstart fired before selectend');
        assert_true(selectStartCount == selectCount,
                    'selectstart fired before select');
        selectStartCount++;
        console.log("Received selectStart: " + selectStartCount);
      }

      function onSelect(ev) {
        assert_equals(ev.inputSource, getExpectedInputSource(ev));
        // select should always be fired between selectstart and selectend.
        assert_true(selectCount + 1 == selectStartCount,
                    'select fired after selectstart');
        assert_true(selectCount == selectEndCount,
                    'select fired after selectend');
        selectCount++;
        console.log("Received onSelect: " + selectCount);
      }

      function onSelectEnd(ev) {
        assert_equals(ev.inputSource, getExpectedInputSource(ev));
        // selectend should always be fired last.
        selectEndCount++;
        assert_true(selectEndCount == selectStartCount,
                    'selectend fired after selectstart');
        assert_true(selectEndCount == selectCount,
                    'selectend fired before select');
        currentIteration++;
        console.log("Received selectEnd: " + selectEndCount);
        console.log("Finished iteration: " + currentIteration + " of " + iterations);
        if (currentIteration == iterations) {
          done();
        } else if (finishAfterEachInput) {
          finishJavaScriptStep();
        }
      }

      function stepSetupListeners(numIterations) {
        iterations = numIterations;
        let currentSession = sessionInfos[sessionTypes.IMMERSIVE].currentSession;
        currentSession.addEventListener('selectstart', onSelectStart, false);
        currentSession.addEventListener('selectend', onSelectEnd, false);
        currentSession.addEventListener('select', onSelect, false);
      }
    </script>
  </body>
</html>
